<template>
  <div>
    <!-- 1.0头部 -->
    <div class="header" v-if="article.type ===1">
      <van-nav-bar
        title="标题"
        left-text="返回"
        left-arrow
        @click-left="$router.push({path:'/'})"
      >
        <template #left>
          <van-icon name="arrow-left" class="arrow-left" />
          <i class="iconfont iconnew"></i>
        </template>
        <template #right>
          <van-button
            round
            :type="article.has_follow ? 'default' : 'danger'"
            size="small"
            @click="floowBten"
            >{{ article.has_follow ? "已关注" : "未关注" }}</van-button
          >
        </template>
      </van-nav-bar>
    </div>
    <!-- 2.1文章主体 -->
    <div class="article-main" v-if="article.type ===1">
      <h1 class="title">{{ article.title }}</h1>
      <p class="author">
        {{ article.user && article.user.nickname }}
        {{ article.user && article.user.create_date | formatDate }}
      </p>
      <article class="author-in" v-html="article.content"></article>
    </div>
    <div v-else class="video-main">
      <video src="" class="video"></video>
      <div class="userinfo">
        <van-image class="avatar" round src=""></van-image>
        <span class="nickname">用户昵称</span>
        <van-button
            round
            :type="article.has_follow ? 'default' : 'danger'"
            size="small"
            @click="floowBten"
            >{{ article.has_follow ? "已关注" : "未关注" }}</van-button
          >
      </div>
    </div>
    <!-- 3.0 点赞和分享  -->
    <div class="operate">
      <div class="btn" @click="dianzanFn">
        <i class="iconfont icondianzan"></i>
        <span>{{article.like_length}}</span>
      </div>
      <div class="btn">
        <i class="iconfont iconweixin"></i>
        <span>微信</span>
      </div>
    </div>
    <!-- 4.0 底部评论 - 组件 -->
    <MyComment :article='article'></MyComment>
  </div>
</template>

<script>
import { getPostDetailstAPI } from "../apis/news.js";
import {
  userFollowsApi,
  userunFollowsApi,
  postLikeByIdsApi, 
} from "../apis/user.js";
import MyComment from "../components/MyComment"
export default {
  components: {
    MyComment,
  },
  methods: {
    //关注
    async floowBten() {
      //有两种情况，已关注，未关注
      if (this.article.has_follow) {
        this.$dialog
          .confirm({
            title: "确定不再关注？",
          })
          .then(async () => {
            const res = await userunFollowsApi(this.article.user.id);
            console.log(res);
            this.$toast.fail(res.data.message);
          });
        this.article.has_follow = !this.article.has_follow;
      } else {
        const res = await userFollowsApi(this.article.user.id);
        this.$toast.success(res.data.message);
        this.article.has_follow = !this.article.has_follow;
      }
    },
    // 点赞
   async dianzanFn(){
    const res = await  postLikeByIdsApi(this.article.id)
    // console.log(res);
    this.article.like_length++
   }
  },
  data() {
    return {
      article: {},
    };
  },
  async created() {
    // console.log(this.$route.params.id);
    const res = await getPostDetailstAPI(this.$route.params.id);
    console.log(res);
    this.article = res.data.data;
  },
};
</script>
    
<style lang="less" scoped>
.header {
  .arrow-left {
    font-size: 20px;
  }
  .iconfont {
    font-size: 50px;
    color: blue;
  }
}
.article-main {
  padding: 10px;
  .title {
    font-size: 18px;
    font-weight: bold;
    margin: 10px 0;
  }

  .author {
    font-size: 14px;
    margin-bottom: 20px;
  }

  .author-in {
    font-size: 14px;
    /deep/ img {
      width: 100%;
    }
    /deep/ p {
      color: red;
      text-indent: 2em;
    }
  }
}
.operate {
  display: flex;
  justify-content: space-around;
  align-items: center;
  height: 100px;
  .btn {
    width: 80px;
    height: 30px;
    border-radius: 15px;
    border: 1px solid #ccc;
    display: flex;
    justify-content:center;
    align-items: center;
    .iconweixin {
      color: chartreuse;
    }
    span {
    }
  }
}
// 视频页面
.video-main {
  .video {
    width: 100%;
    height: 300px;
    background: rgb(0, 0, 0);
  }

  .userinfo {
    display: flex;
    padding: 0 40px;
    height: 50px;
    align-items: center;
    .avatar {
      background: salmon;
      width: 20px;
      height: 20px;
    }

    .nickname {
      flex: 1;
    }
  }
}
</style>